package com.ptmall.app.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ptmall.app.R;
import com.ptmall.app.bean.HomePageTuijianBean;
import com.ptmall.app.bean.ProductBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.CateListActivity;
import com.ptmall.app.ui.activity.GoodDetilActivity;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by zjh on 2016/9/23.
 * 新品推荐
 */
public class RecyCommendAdapter extends RecyclerView.Adapter<RecyCommendAdapter.ViewHolder> {

    GoodListAdapter mGoodListAdapter;
    private Activity mActivity;
    private List<ProductBean> mData = new ArrayList();

    public interface Click {
        void addcars(ImageView iv, String id, String num);

    }

    public Click click;

    public void setClick(Click click) {
        this.click = click;
    }

    public RecyCommendAdapter(Activity mActivity) {
        this.mActivity = mActivity;
    }

    public void bind(List<ProductBean> mData) {
        this.mData = mData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_homegood, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int position) {
        ProductBean bean = mData.get(position);

//        viewHolder.goodname.setText(bean.product_name);
//        viewHolder.sellprice.setText("￥" + bean.sell_price);
//        GlideUtil.showImg(mActivity, bean.list_img, viewHolder.img);
//
//        if (bean.is_tejia == null || bean.is_tejia.equals("1")) {
//            viewHolder.activi_rel.setVisibility(View.GONE);
//        } else {
//            viewHolder.activi_rel.setVisibility(View.VISIBLE);
//        }
//        if (!TextUtils.isEmpty(bean.manjian)) {
//            viewHolder.activi_rel.setVisibility(View.VISIBLE);
//            viewHolder.activi_text.setText(bean.manjian);
//        }
//        viewHolder.img.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(mActivity, GoodDetilActivity.class);
//                intent.putExtra("id", bean.productId);
//                mActivity.startActivity(intent);
//            }
//        });
//        viewHolder.addcar.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                click.addcars(viewHolder.img, bean.productId, "1");
////                addcar(bean.productId,"1");
//            }
//        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView activi_text, goodname, sellprice;
        ImageView img, addcar;
        RelativeLayout activi_rel;

        public ViewHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.img);
            addcar = (ImageView) view.findViewById(R.id.addcar);
            activi_rel = (RelativeLayout) view.findViewById(R.id.activi_rel);
            activi_text = (TextView) view.findViewById(R.id.activi_text);
            goodname = (TextView) view.findViewById(R.id.goodname);
            sellprice = (TextView) view.findViewById(R.id.sellprice);
        }
    }


    private void addcar(String id, String num) {
        Map<String, String> map = new HashMap<>();
        map.put("productId", id);
        map.put("num", num);
        ApiDataRepository.getInstance().addShopCar(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                Toast.makeText(mActivity, "加入购物车成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

}
